﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ChinessChess
{
   public class CBanCo
    {
        #region Variable
        Pen BrownColor = new Pen(Brushes.SlateBlue, 1);
        Pen BlueColor = new Pen(Brushes.Black, 1);
        Graphics cdc = null;
        int num = 0;
        Point[] m_Point = new Point[90];
        #endregion Variable

        #region Properties
        //Tất cả các điểm trên bàn cờ
        public Point[] POINTS
        {
            get { return m_Point; }
        }
        
        public Region REGION
        {
            get
            {
                Rectangle rc = new Rectangle(0, 0, Program.size, Program.size);
                System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
                path.AddEllipse(rc);
                Region rg = new Region(path);
                return rg;
            }
        }
        #endregion Properies

        public CBanCo(Graphics g)
        {
            cdc = g;
            cdc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
           
        }

        public CBanCo()
        {
        }
        
        public void DrawChessBoard()
        {
            Program.endX=Program.beginX+Program.kc*(Program.column-1);
            Program.endY=Program.beginY+ Program.kc*(Program.row-1);
            int nuaTren= Program.beginY + Program.kc * (Program.row/2-1);
            cdc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (cdc != null)
            {
                //Ngang 10
               
                for (int i = 0; i < Program.row; i++) {
                    cdc.DrawLine(BlueColor, new Point(Program.beginX, Program.beginY + i * Program.kc),
                        new Point(Program.endX,Program.beginY + i * Program.kc));
                }
                    //Doc 9
                for (int j = 0; j < Program.column; j ++)
                {
                    if (j == 0 || j == Program.column - 1) {
                        cdc.DrawLine(BlueColor, new Point(Program.beginX + Program.kc * j, Program.beginY),
                         new Point(Program.beginX + Program.kc * j, Program.endY));
                    }
                    else
                        cdc.DrawLine(BlueColor, new Point(Program.beginX + Program.kc*j,Program.beginY),
                            new Point(Program.beginX + Program.kc * j, nuaTren));
                }
                for (int j =0; j < Program.column; j ++)
                {
                    cdc.DrawLine(BlueColor, new Point(Program.beginX + Program.kc * j, nuaTren+Program.kc),
                        new Point(Program.beginX + Program.kc * j, Program.endY));
                }
                
                //ve song
                cdc.DrawLines(BrownColor, new[] 
                { 
                    new Point(Program.beginX,nuaTren),                     
                    new Point(Program.beginX+2*Program.kc,nuaTren+Program.kc),                    
                    new Point(Program.beginX+4*Program.kc,nuaTren),                    
                    new Point(Program.beginX+6*Program.kc,nuaTren+Program.kc),                    
                    new Point(Program.beginX+8*Program.kc,nuaTren)
                });
                cdc.DrawLines(BrownColor, new[] 
                { 
                    new Point(Program.beginX,nuaTren+Program.kc),                     
                    new Point(Program.beginX+2*Program.kc,nuaTren),                    
                    new Point(Program.beginX+4*Program.kc,nuaTren+Program.kc),                    
                    new Point(Program.beginX+6*Program.kc,nuaTren),                    
                    new Point(Program.beginX+8*Program.kc,nuaTren+Program.kc)
                });
                //Cung trên
                cdc.DrawLine(BrownColor, new Point(Program.beginX+ 3 * Program.kc, Program.beginY),
                    new Point(Program.beginX + 5 * Program.kc, Program.beginY + 2 * Program.kc));
                cdc.DrawLine(BrownColor, new Point(Program.beginX + 5 * Program.kc,Program.beginY),
                    new Point(Program.beginX + 3 * Program.kc, Program.beginY + 2 * Program.kc));
               // Cung dưới
                cdc.DrawLine(BrownColor, new Point(Program.beginX + 3 * Program.kc, Program.endY-2*Program.kc), new Point(Program.beginX + 5 * Program.kc, Program.endY));
                cdc.DrawLine(BrownColor, new Point(Program.beginX + 3 * Program.kc, Program.endY), new Point(Program.beginX + 5 * Program.kc, Program.endY-2*Program.kc));
                
                DrawGrid();
            }
        }

        //Luu toa do cac diem tren ban co 
        private void DrawGrid()
        {
            for (int i = 0; i < Program.row; i++) {
                for (int j = 0; j < Program.column; j++) {
                    Point p = new Point(Program.beginX + j*Program.kc, Program.beginY + i*Program.kc);
                    m_Point[num] = p;
                    num++;
                }
            }
        }
        public void SnapToPoint(ref int X, ref int Y)
        {
            X = ((Program.kc * int.Parse((X / Program.kc).ToString())) - 10) + Program.kc;
            Y = ((Program.kc * int.Parse((Y / Program.kc).ToString())) - 10) + Program.kc;
        }
       public int index(Point p){
            int vt=-1;
            for (int i = 0; i < num; i++) {
                if (m_Point[i] == p) {
                    return i;
                }
            }
            return vt;
       }
       public bool CheckPoint(Point p)
       {
           foreach (Point pt in POINTS)
               if (p == pt)
                   return true;
           return false;
       }
        public void DrawRect(Graphics dc, Point point)
        {
            Rectangle rect = Rectangle.Empty;
            rect.Location = new Point(point.X - 25, point.Y - 25);
            rect.Size = new Size(50, 50);
            dc.DrawRectangle(Pens.Red, rect);
        }
    }
}
